import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JPasswordField;
import javax.swing.JSplitPane;
import javax.swing.JInternalFrame;


public class LogIn extends JFrame {
	public LogIn(){
	getContentPane().add(new Panel());
	setPreferredSize(new Dimension(800, 350));
	pack();
	setVisible(true);
	setTitle("Log In");
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	}




    private class Panel extends JPanel {
	private JTextField textField;
	private JPasswordField passwordField;

	/**
	 * Create the panel.
	 */
	public Panel() {
		setLayout(null);
		setBackground(Color.ORANGE);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(46, 95, 105, 16);
		add(lblUsername);
		JLabel lblNewLabel_5 = new JLabel("");
		lblNewLabel_5.setIcon(new ImageIcon(LogIn.class.getResource("/resources/p-331002-georgia-tech-yellow-jackets-black-spare-tire-cover-jt-2324558420.jpg")));
		lblNewLabel_5.setBounds(600, 5, 201, 164);
		getContentPane().add(lblNewLabel_5);
		
		textField = new JTextField();
		textField.setBounds(163, 89, 222, 28);
		add(textField);
		textField.setColumns(10);
		
		JButton btnCreateAccount = new JButton("Create Account");
		btnCreateAccount.setBounds(245, 187, 151, 29);
		add(btnCreateAccount);
		
		btnCreateAccount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
               dispose();
                
                javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    
                	public void run() {
                        new createAccount();
 
                    }
                });
            }
		});
            
	
                
            
		
		JButton btnLogin = new JButton("Login");
		btnLogin.setBounds(421, 187, 145, 29);
		add(btnLogin);
		
		btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
            	
            	try {
            	 Connection con =Connect.getConnection();
            	 Statement select =con.createStatement();
            	 
            	 char check='\0';

            	 ResultSet result = select.executeQuery("SELECT Username,Password FROM STUDENT ");
            	 while (result.next()) { // process results one row at a time
            	       if(result.getString(1).equals(textField.getText()) && result.getString(2).equals(new String(passwordField.getPassword()))){
            	    	   check='S';
            	    	   break;
            	       }
            	      
            	 }
            	 result=select.executeQuery("SELECT Username,Password FROM FACULTY ");
            	 while (result.next()) { // process results one row at a time
            	       if(result.getString(1).equals(textField.getText()) && result.getString(2).equals(new String(passwordField.getPassword()))){
            	    	   check='F';
            	    	   break;
            	       }
            	 }
            	 result=select.executeQuery("SELECT Username,Password FROM ADMIN ");
            	 while (result.next()) { // process results one row at a time
            	       if(result.getString(1).equals(textField.getText()) && result.getString(2).equals(new String(passwordField.getPassword()))){
            	    	   check='A';
            	    	   break;
            	       }
            	 }
            	 
            	 
            	 
                    if(check=='F'){
                    	dispose();
                    	new HomePageFaculty(textField.getText());
                    }
                    else if(check=='A'){
                    	dispose();
                    	new AdminServices(textField.getText());
                    }
                    else if(check=='S'){
                    	dispose();
                    	new HomePageStudent(textField.getText());	
                    } else{
            		 JOptionPane.showMessageDialog(null,"Incorrect Log-In credentials, please try again!");
                    }
                   
            	if(!con.isClosed())
            	System.out.println("Successfully connected to " +
            	"MySQL server using TCP/IP...");
            	} catch(Exception eb) {
            		JOptionPane.showMessageDialog(getContentPane(),"Check Connection");
            	} 
            	}
            
		});
		
		passwordField = new JPasswordField();
		passwordField.setBounds(163, 133, 222, 28);
		add(passwordField);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(46, 139, 61, 16);
		add(lblPassword);
		
		JLabel lblGtport = new JLabel("GTPort");
		lblGtport.setFont(new Font("Arial", Font.BOLD, 17));
		lblGtport.setBounds(311, 19, 61, 16);
		add(lblGtport);

	}
}
}
